package xyz.yuzegod.megawalls.util;

import net.minecraft.server.v1_8_R3.Packet;
import net.minecraft.server.v1_8_R3.PacketPlayOutScoreboardTeam;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;

public class NameTagUtils
{
    public static final Field TEAM_NAME;
    public static final Field DISPLAY_NAME;
    public static final Field PREFIX;
    public static final Field SUFFIX;
    public static final Field PARAM_INT;
    public static final Field PACK_OPTION;
    public static final Field MEMBERS;
    
    public static PacketPlayOutScoreboardTeam createPacket(final String name, final String prefix, final String suffix) {
        final PacketPlayOutScoreboardTeam packet = new PacketPlayOutScoreboardTeam();
        setName(packet, name);
        setDisplayName(packet, name);
        setPrefix(packet, prefix);
        setSuffix(packet, suffix);
        setPackOption(packet);
        return packet;
    }
    
    public static void sendPacket(final Player reciver, final PacketPlayOutScoreboardTeam packet) {
        final CraftPlayer c = (CraftPlayer)reciver;
        c.getHandle().playerConnection.sendPacket((Packet)packet);
    }
    
    public static void setName(final PacketPlayOutScoreboardTeam packet, final String teamName) {
        set(NameTagUtils.TEAM_NAME, packet, teamName);
    }
    
    public static void setDisplayName(final PacketPlayOutScoreboardTeam packet, final String teamDisplayName) {
        set(NameTagUtils.DISPLAY_NAME, packet, teamDisplayName);
    }
    
    public static void setPrefix(final PacketPlayOutScoreboardTeam packet, final String teamPrefix) {
        set(NameTagUtils.PREFIX, packet, teamPrefix);
    }
    
    public static void setSuffix(final PacketPlayOutScoreboardTeam packet, final String teamSuffix) {
        set(NameTagUtils.SUFFIX, packet, teamSuffix);
    }
    
    public static void setPararmInt(final PacketPlayOutScoreboardTeam packet, final int paramInt) {
        set(NameTagUtils.PARAM_INT, packet, paramInt);
    }
    
    public static void setPackOption(final PacketPlayOutScoreboardTeam packet) {
        set(NameTagUtils.PACK_OPTION, packet, 1);
    }
    
    public static void addMember(final PacketPlayOutScoreboardTeam packet, final String player) {
        try {
            ((List)NameTagUtils.MEMBERS.get(packet)).add(player);
        }
        catch (IllegalArgumentException ex) {}
        catch (IllegalAccessException ex2) {}
    }
    
    public static void set(final Field field, final Object object, final Object value) {
        try {
            field.set(object, value);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static Field getAccessibleField(final Class<?> clazz, final String fieldName) {
        try {
            final Field field = clazz.getDeclaredField(fieldName);
            makeAccessible(field);
            return field;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static void makeAccessible(final Field field) {
        if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
            field.setAccessible(true);
        }
    }
    
    static {
        TEAM_NAME = getAccessibleField(PacketPlayOutScoreboardTeam.class, "a");
        DISPLAY_NAME = getAccessibleField(PacketPlayOutScoreboardTeam.class, "b");
        PREFIX = getAccessibleField(PacketPlayOutScoreboardTeam.class, "c");
        SUFFIX = getAccessibleField(PacketPlayOutScoreboardTeam.class, "d");
        PARAM_INT = getAccessibleField(PacketPlayOutScoreboardTeam.class, "h");
        PACK_OPTION = getAccessibleField(PacketPlayOutScoreboardTeam.class, "i");
        MEMBERS = getAccessibleField(PacketPlayOutScoreboardTeam.class, "g");
    }
}
